from sklearn.datasets import fetch_lfw_people
import matplotlib.pyplot as plt

def plot_gallery(images, names, height, width, n_col, n_row, cmap=plt.cm.gray):
    fig, axs = plt.subplots(n_row,n_col, figsize=(22,9), subplot_kw={'xticks':(), 'yticks':()})
    for i, image in zip(range(n_row*n_col), images):
        r = int(i/n_col); c = i%n_col
        axs[r,c].imshow(image.reshape((h, w)), cmap=cmap)
        axs[r,c].set_title(names[y[i]])
            
lfw = fetch_lfw_people()
X = lfw.data
y = lfw.target

n_samples, height, width = lfw.images.shape
n_features = X.shape[1]
names = lfw.target_names

plot_gallery(X, names, height, width, 8, 3)